package com.basic.modules.ykf.controller;

import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.common.annotation.StringTrim;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.validator.ValidatorUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfCustomPostCommissionEntity;
import com.basic.modules.ykf.service.YkfCustomPostCommissionService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;


/**
 * 店铺子账号分配记录
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-07-03 14:32:10
 */
@RestController
@RequestMapping("ykf/customPostCommission")
public class YkfCustomPostCommissionController {

    @Autowired
    private YkfCustomPostCommissionService ykfCustomPostCommissionService;

    /**
     * 列表-分页
     */
    @StringTrim
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfCustomPostCommissionEntity> params){
        PageUtils page = ykfCustomPostCommissionService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfCustomPostCommissionEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getPostCommissionId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfCustomPostCommissionService.getById(params.getPostCommissionId()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfCustomPostCommissionEntity ykfCustomPostCommission){
        ValidatorUtils.validateEntity(ykfCustomPostCommission);
        QueryWrapper<YkfCustomPostCommissionEntity> queryWrapper = new QueryWrapper<>();
        if (StrUtil.equals(ykfCustomPostCommission.getCompanyNo(), "ALL")) {
            queryWrapper.lambda().eq(YkfCustomPostCommissionEntity::getCustomPostType, ykfCustomPostCommission.getCustomPostType())
                    .eq(YkfCustomPostCommissionEntity::getCompanyNo, ykfCustomPostCommission.getCompanyNo());
            if (ykfCustomPostCommissionService.count(queryWrapper) > 0) {
                throw new RRException("总公司客服岗位" + ykfCustomPostCommission.getCustomPostTypeName() + "已存在，请检查");
            }
        } else {
            queryWrapper.lambda().eq(YkfCustomPostCommissionEntity::getCompanyNo, ykfCustomPostCommission.getCompanyNo());
            if (ykfCustomPostCommissionService.count(queryWrapper) > 0) {
                throw new RRException("分公司" + ykfCustomPostCommission.getCompanyName() + "已存在提成系数，请检查");
            }
        }
		ykfCustomPostCommissionService.save(ykfCustomPostCommission);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfCustomPostCommissionEntity ykfCustomPostCommission){
        ValidatorUtils.validateEntity(ykfCustomPostCommission);
        QueryWrapper<YkfCustomPostCommissionEntity> queryWrapper = new QueryWrapper<>();
        if (StrUtil.equals(ykfCustomPostCommission.getCompanyNo(), "ALL")) {
            queryWrapper.lambda().eq(YkfCustomPostCommissionEntity::getCustomPostType, ykfCustomPostCommission.getCustomPostType())
                    .eq(YkfCustomPostCommissionEntity::getCompanyNo, ykfCustomPostCommission.getCompanyNo())
                    .ne(YkfCustomPostCommissionEntity::getPostCommissionId, ykfCustomPostCommission.getPostCommissionId());
            if (ykfCustomPostCommissionService.count(queryWrapper) > 0) {
                throw new RRException("总公司客服岗位" + ykfCustomPostCommission.getCustomPostTypeName() + "已存在，请检查");
            }
        } else {
            queryWrapper.lambda().eq(YkfCustomPostCommissionEntity::getCompanyNo, ykfCustomPostCommission.getCompanyNo())
                    .ne(YkfCustomPostCommissionEntity::getPostCommissionId, ykfCustomPostCommission.getPostCommissionId());
            if (ykfCustomPostCommissionService.count(queryWrapper) > 0) {
                throw new RRException("分公司" + ykfCustomPostCommission.getCompanyName() + "已存在提成系数，请检查");
            }
        }
		ykfCustomPostCommissionService.updateById(ykfCustomPostCommission);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] postCommissionIds){
		ykfCustomPostCommissionService.removeByIds(Arrays.asList(postCommissionIds));
        return R.ok();
    }

}
